package controler;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import model.Assistant;
import model.beans.MinutaBean;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.DynaActionForm;
import org.apache.struts.actions.MappingDispatchAction;

import business.misc.Util;

public class AssistantAction extends MappingDispatchAction {
	private final String DEFAULT = "default";
	
	/**
	 * Listado de asistentes por minuta
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return ActionForward
	 * @throws Exception
	 * 
	 * @author Daniel Valencia
	 */
	public ActionForward list(
			ActionMapping mapping, 
			ActionForm form, 
			HttpServletRequest request, 
			HttpServletResponse response) throws Exception {
			
			ActionForward action_retVal = Validate.validateUser(mapping,request, getResources(request));
			if(action_retVal!=null)return action_retVal;
			
			HttpSession session = request.getSession();
			MinutaBean minuta_bean = (MinutaBean) session.getAttribute("bean_minuta");
			int minuta_id = minuta_bean.getInt_tk();
			
			request.setAttribute("assistant_lst", Assistant.list(minuta_id));
			
		return mapping.findForward(DEFAULT);
	}
	
	public ActionForward delete(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
		ActionForward action_retVal = Validate.validateUser(mapping,request, getResources(request));
		if(action_retVal!=null)return action_retVal;		
		
		HttpSession session = request.getSession();
		MinutaBean minuta_bn = (MinutaBean)session.getAttribute("bean_minuta");
		int minuta_id = minuta_bn.getInt_tk();
		
		String id_asistente = request.getParameter("id_asistente");
		
		if( Assistant.delete(Integer.parseInt(id_asistente))){
			request.setAttribute("req_msg", getResources(request).getMessage("DraftManage.SuccessDelete"));
		}else{
			request.setAttribute("req_msg", getResources(request).getMessage("Assistants.FailDelete"));
		}
		request.setAttribute("assistant_lst", model.Assistant.list(minuta_id));
		return mapping.findForward(Util.DEFAULT);
	}
	
	public ActionForward add(
			ActionMapping mapping, 
			ActionForm form, 
			HttpServletRequest request, 
			HttpServletResponse response) throws Exception {
		
			ActionForward action_retVal = Validate.validateUser(mapping,request, getResources(request));
			if(action_retVal!=null)return action_retVal;
		
			DynaActionForm forma = (DynaActionForm)form;
			forma.initialize(mapping);
			forma.reset(mapping, request);
			
		return mapping.findForward(DEFAULT);
	}
	
	public ActionForward save(
			ActionMapping mapping, 
			ActionForm form, 
			HttpServletRequest request, 
			HttpServletResponse response) throws Exception {
		
			ActionForward action_retVal = Validate.validateUser(mapping,request, getResources(request));
			if(action_retVal!=null)return action_retVal;
			
			String str_forward;
			
			HttpSession session = request.getSession();
			MinutaBean minuta_bn = (MinutaBean)session.getAttribute("bean_minuta");
			int minuta_id=minuta_bn.getInt_tk();
			DynaActionForm forma = (DynaActionForm)form;
							
			if( model.Assistant.add(forma.getMap(),minuta_id) > 0 ){
				request.setAttribute("req_msg", getResources(request).getMessage("DraftManage.SuccessAdd"));
				str_forward = Util.SUCCESS;
			}else{
				request.setAttribute("req_msg", getResources(request).getMessage("DraftManage.FailAdd"));
				str_forward = Util.FAIL+Util.ADD;
			}			
			
			if(str_forward.equals(Util.SUCCESS)){
				request.setAttribute("assistant_lst", model.Assistant.list(minuta_id));				
			}
			request.setAttribute("draft_id",new Integer(minuta_id));
		return mapping.findForward(str_forward);
	}

}
